package Controller.Sign;

import Dao.Sign.DeviceLrDao;
import Model.ElderInfo;
import Model.SignModel.DeviceArrangeLr;
import Model.SignModel.DeviceLr;
import Model.User;
import Service.DeviceArrangeLrService;
import Service.DeviceLrService;
import Service.ElderInfoService;
import Service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.permission.ConstantsUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/DeviceArrangeLr")
public class DeviceArrangeLrController {

    @Autowired
    private DeviceArrangeLrService deviceArrangeLrService;
    @Autowired
    private ElderInfoService elderInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private DeviceLrService deviceLrService;
    @Autowired
    private DeviceLrDao deviceLrDao;

    @RequestMapping(value = "/getDeviceArrangeLrList")
    @ResponseBody
    public PageInfo getDeviceArrangeLrList(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam(value = "limit", defaultValue = "10") Integer limit, HttpServletRequest request, DeviceArrangeLr deviceArrangeLr){
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String Role_id=this.userService.findRoleIDbyUserNumber(user.getUser_number());
        if (Role_id.equals("2")){
            String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
            System.out.println("找到机构代码："+settleCode);
            deviceArrangeLr.setOrganization_id(settleCode);
            PageHelper.startPage(pn, limit);
            List<DeviceArrangeLr> list = deviceArrangeLrService.getDeviceArrangeLrList(deviceArrangeLr);
            System.out.println("[获取到发放老人设备的信息：]"+list);
            PageInfo page = new PageInfo(list);
            return page;
        }else{
            PageHelper.startPage(pn, limit);
            List<DeviceArrangeLr> list = deviceArrangeLrService.getDeviceArrangeLrList(deviceArrangeLr);
            System.out.println("[获取到发放老人设备的信息：]"+list);
            PageInfo page = new PageInfo(list);
            return page;
        }
    }

    @RequestMapping(value = "/deleteDeviceArrangeLr")
    @ResponseBody
    public int deleteDeviceArrangeLr(int id){
        int status = 0;
        try {
            DeviceArrangeLr deviceArrangeLr = new DeviceArrangeLr();
            deviceArrangeLr.setId(id);
            //首先获取要删除的发放记录的设备号
            List<DeviceArrangeLr> list = deviceArrangeLrService.getDeviceArrangeLrList(deviceArrangeLr);
            System.out.println("当前删除发放记录的设备号是："+list.get(0).getDevice_lr());
            DeviceLr deviceLr = new DeviceLr();
            deviceLr.setDevice_lr(list.get(0).getDevice_lr());
            deviceLrDao.updateStateByDevice(deviceLr);
            System.out.println("删除设备后更新设备状态为未使用！");
            deviceArrangeLrService.deleteDeviceArrangeLr(id);
            System.out.println("[删除发放老人设备记录完成~]");

        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping(value = "/updateDeviceArrangeLr")
    @ResponseBody
    public int updateDeviceArrangeLr(DeviceArrangeLr deviceArrangeLr){
        int status = 0;
        try {
            deviceArrangeLrService.updateDeviceArrangeLr(deviceArrangeLr);
            System.out.println("[更新发放老人设备完成~]");
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping(value = "/addDeviceArrangeLr")
    @ResponseBody
    public int addDeviceArrangeLr(HttpServletRequest request, DeviceArrangeLr deviceArrangeLr, ElderInfo elderInfo){
        int status = 0;
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
        System.out.println("当前机构ID为："+settleCode);
        deviceArrangeLr.setOrganization_id(settleCode);
        String organization = user.getUser_name();
        System.out.println("当前机构为："+organization);
        deviceArrangeLr.setOrganization(organization);

        elderInfo.setId_number(request.getParameter("lr_id_number"));
        try {
            deviceArrangeLrService.addDeviceArrangeLr(deviceArrangeLr);
            System.out.println("[添加发放老人设备完成~]");
            System.out.println("当前发放的设备号是："+deviceArrangeLr.getDevice_lr());
            DeviceLr deviceLr = new DeviceLr();
            deviceLr.setDevice_lr(deviceArrangeLr.getDevice_lr());
            deviceLrDao.updateStateByDevice(deviceLr);//设备发放后设置设备状态为已使用
            elderInfoService.updateLrDevice(elderInfo);
            System.out.println("[同步发放老人设备完成~]");
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }
}
